package bhbt.controller.admin.member;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Member;
import bhbt.service.MemberService;
import bhbt.taglib.pagetag.Pagination;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class MemberActiveController extends BaseController {
	@Resource
    private MemberService memberService;
   
   

    @RequestMapping("admin/member/memberActiveShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	
        	//方法一
        	//Map<String, Object> member = memberService.queryForObject("getJoinByKey", id);
        	//request.setAttribute("member", member);
        	
        	//方法二
        	formMap.put("id", id);	//未激活
        	List<Member> list = memberService.queryForList("memberForList",formMap);
        	
        	if(list.size()>0){
        		 
            	//member=list.get(0);
            	request.setAttribute("member", list.get(0));
        	}
        	
        }
        return "admin/member/memberActive";
       
    }
    
    
    @RequestMapping("admin/member/memberActive.shtml")
    public String update(HttpServletRequest request, ModelMap model) throws Exception{
        
    	
    	Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
        
        boolean result =true;  //验证结果
        if(0 < id){
        	Admin admin = ContextUtil.getAdmin();
        	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
        	 
        	if(!admin.getTwoPassword().equals(pwd)){
        		setMessage(model,"管理员安全密码不正确。");
        		result=false;
        		 
        		
        	}else{
        		
        		Member member = memberService.getByKey(id);
        		 
        		//推荐人验证
        		if(member.getRecommendId()!=null){
        			
        			Member recommend = memberService.getByKey(member.getRecommendId());
        			if(recommend!=null){
        				if(recommend.getAccountActive().equals("F")){
        					setMessage(model,"推荐人未激活。");
        					result=false;
        				}
        			}
        			 
        		}
        		
        		
        		if(member.getFatherId()!=null){
        			Member father = memberService.getByKey(member.getRecommendId());
        			if(father!=null){
        				if(father.getAccountActive().equals("F")){
        					 
        					setMessage(model,"该接点人未激活，请先激活推荐人。");
        					result=false;
        				}
        			}
        			 
        		}
        	} 
        	if(!result){
        		Map<String, Object> member = memberService.queryForObject("getJoinByKey", id);
            	
            	request.setAttribute("member", member); 
        		return "admin/member/memberActive";
    		}
        	 
        	memberService.saveActive(id,null);
        	 
        }
        return "redirect:memberYesActiveList.shtml";
    }

     
    

}
